#include "SysMainForm.h"
#include "ui_SysMainForm.h"
#include "SysDAL.h"
#include "SysOpenCV.h"
#include "SysRegisterDialog.h"
#include "SysAddAdminDialog.h"

#include <QPainter>
#include <QMessageBox>
#include <QtDebug>
#include <QMenu>
#include <QMenuBar>
#include <QAction>
#include <QTimer>
#include <QAbstractItemModel>
#include <QDateTime>
#include <QUrl>
#include <QDesktopServices>


MainForm::MainForm(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainForm)
{
    ui->setupUi(this);
    this->setWindowState(Qt::WindowMaximized);
//        添加菜单栏
    QMenuBar *mBar=menuBar();
//        添加菜单
    QMenu *Setting=mBar->addMenu("系统管理");
//        添加菜单项
    QAction *setting=Setting->addAction("系统设置");
    QAction *info=Setting->addAction("系统信息");
    QAction *admin=Setting->addAction("系统管理员");
//        QMenu *pEdit=mBar->addMenu("编辑");
//        QMenu *pView=mBar->addMenu("视图");
    QMenu *pAbout=mBar->addMenu("关于");
    QAction *pGit=pAbout->addAction("github");
    QAction *pAboutQt=pAbout->addAction("关于QT");
    auto *pConnMe=mBar->addAction("系统简介");
    auto *quit=mBar->addAction("退出");
    connect(pGit,&QAction::triggered,[=](){
        QUrl Url("https://gitee.com/zcj2019/TRMS");
        QDesktopServices::openUrl(Url);
    });
    connect(pAboutQt,&QAction::triggered,[=](){
        QMessageBox qt;
        qt.aboutQt(this);
    });
    connect(admin,&QAction::triggered,this,&MainForm::SysAdmin);
    connect(pConnMe,&QAction::triggered,[=](){
        QMessageBox *mes=new QMessageBox (this);
        mes->setWindowTitle("系统简介");
        mes->setText("学生只需通过一张身份证或者学生证就能短时间完成登记，要求操作简单快速，同时自动辨别该同学是否满足登记要求，以及分配位置并反馈给该同学，自动监视学生签到情况，如缺勤做好记录，缺勤达到一定次数自动注销该同学位置，管理者能通过该系统查看位置申请情况，方便需要的同学申请。");
        mes->show();
    });
    connect(quit,&QAction::triggered,[=](){
        QMessageBox msgBox;
        msgBox.setWindowTitle(QObject::tr("提示"));
        msgBox.setText(QObject::tr("你确认退出系统吗？？"));
        msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
        msgBox.setDefaultButton(QMessageBox::Ok);
        int ret = msgBox.exec();
        if(ret == QMessageBox::Ok)
        {
            this->close();
        }
    });

//    const QString MyStyle ="background: rgba(255,255,255, 0.85);color:black";
//    const QString MyStyle1 ="background: rgba(0,0,0, 0.45)";
//    ui->tableView->setStyleSheet(MyStyle);
//    ui->textBrowser->setStyleSheet(MyStyle);
    ui->stackedWidget->setCurrentIndex(2);
//    ui->groupBox->setStyleSheet(MyStyle1);
    TableRefresh();

//研修室管理面板，通过QLCDNumber显示位置数
    ClassInfo seat_102=SysDBL::getSeatNum(102);
    ClassInfo seat_108=SysDBL::getSeatNum(108);
    ClassInfo seat_601=SysDBL::getSeatNum(601);
    ui->lcd102->display(seat_102.Amount);
    ui->lcd102Empty->display(seat_102.Empty);
    ui->lcd102Used->display(seat_102.Used);

    ui->lcd108->display(seat_108.Amount);
    ui->lcd108Empty->display(seat_108.Empty);
    ui->lcd108Used->display(seat_108.Used);

    ui->lcd601->display(seat_601.Amount);
    ui->lcd601Empty->display(seat_601.Empty);
    ui->lcd601Used->display(seat_601.Used);
//登记管理模块，设置显示学生基本信息的lable背景色

}

MainForm::~MainForm()
{
    delete ui;

}


void MainForm::paintEvent(QPaintEvent *event)
{

    QPainter p(this);
    p.drawPixmap(0,0,this->width(),this->height(),QPixmap(":/Sys/SysPicture/1.jpg"));

}

void  MainForm::TableRefresh()
{

    QSqlTableModel *model=new QSqlTableModel (ui->stackedWidget->currentWidget());
    model->setTable("Admin");
    model->select();
    TableHeader << QObject::tr("账号") << QObject::tr("密码") << QObject::tr("姓名") << QObject::tr("性别") << QObject::tr("年龄")
                << QObject::tr("身份证号") << QObject::tr("联系方式") << QObject::tr("操作时间") << QObject::tr("权限");
    for(int i = 0 ; i < TableHeader.count(); i++)
    {
        model->setHeaderData(i,Qt::Horizontal,TableHeader[i]);//设置显示框表头显示
    }
    ui->AdminTableView->setModel(model);

}

//void MainForm::on_pushButton_clicked()
//{

//    ui->stackedWidget->setCurrentIndex(0);
//    this->setWindowTitle(QObject::tr("签到管理"));

//}

void MainForm::on_pushButton_2_clicked()
{

    ui->stackedWidget->setCurrentIndex(1);
    this->setWindowTitle(QObject::tr("研修室管理"));
}

void MainForm::on_pushButton_3_clicked()
{

    ui->stackedWidget->setCurrentIndex(2);
    this->setWindowTitle(QObject::tr("登记管理"));
}

void MainForm::SysAdmin()
{
    ui->stackedWidget->setCurrentIndex(3);
}
void MainForm::on_Btn102Info_clicked()
{
    SeatInfoDialog *SeatInfo=new SeatInfoDialog ();
    SeatInfo->open1();
}

void MainForm::on_Btn108Info_clicked()
{
    SeatInfoDialog *SeatInfo=new SeatInfoDialog ();
    SeatInfo->open2();
}

void MainForm::on_Btn601info_clicked()
{
    SeatInfoDialog *SeatInfo=new SeatInfoDialog ();
    SeatInfo->open3();
}

void MainForm::on_BtnStuReg_clicked()
{
    SysRegisterDialog *StuReg=new SysRegisterDialog (this);

    StuReg->exec();

}

void MainForm::on_BtnAddAdmin_clicked()
{
    addAdmin *add =new addAdmin(this);
    add->exec();
}

void MainForm::on_pushButton_8_clicked()
{
    TableRefresh();
}

void MainForm::on_pushButton_6_clicked()
{
    QStringList up;
    int i,curRow=ui->AdminTableView->currentIndex().row(); //获取tableview选中的行索引值
    if(curRow==-1)
    {
        QMessageBox::warning(this,QObject::tr("提示"),QObject::tr("请选择一行数据"));

    }
    else
    {
//        int curCol=ui->AdminTableView->currentIndex().column();//获取tableview选中的列索引值
        int colnum=ui->AdminTableView->model()->columnCount();//获取tableview列数
        for(i = 0; i < colnum; i++)
        {
            QAbstractItemModel *modessl = ui->AdminTableView->model();
            QModelIndex indextemp = modessl->index(curRow,i);//遍历第一行的所有列 ,i是你要取值的列的下标
            QVariant datatemp = modessl->data(indextemp);
            up<<datatemp.toString();
        }
        UpDialog=new SysUpAdminDialog();
        UpDialog->getArg(up);
        UpDialog->exec();

    }
}

void MainForm::on_pushButton_7_clicked()
{
    int curRow=ui->AdminTableView->currentIndex().row();
    if(curRow==-1)
    {
        QMessageBox::warning(this,QObject::tr("提示"),QObject::tr("请选择一行数据"));

    }
    else
    {
        QAbstractItemModel *modessl = ui->AdminTableView->model();
        QModelIndex indexID = modessl->index(curRow,0);
        QModelIndex indexName = modessl->index(curRow,0);
        QString getID = modessl->data(indexID).toString();
        QString getName=modessl->data(indexName).toString();

        QMessageBox msgBox;
        msgBox.setText(QObject::tr("尊敬的超级管理员，你好！"));
        msgBox.setInformativeText(QObject::tr("请确认一下，您确实要删除 %1 吗?").arg(getName));
        msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
        msgBox.setDefaultButton(QMessageBox::Ok);
        int ret = msgBox.exec();
        if(ret == QMessageBox::Ok)
        {
            ExecStatus modify=SysDBL::AdminRemove(getID);
            if(modify.Status)
            {
                QMessageBox::warning(this,QObject::tr("提示"),QObject::tr("账号：%1 ，姓名：%2 移除成功").arg(getID).arg(getName));
            }
            else
            {
                QMessageBox::warning(this,QObject::tr("提示"),QObject::tr("删除失败，错误反馈如下：\n")+modify.StatusInfo);
            }
            TableRefresh();
        }
    }
}


void MainForm::on_BtnQuery_clicked()
{
    QString stuid=ui->lineStuId->text();
    QStringList GetInfo = SysDBL::regInfo(stuid),TableHeader;
    if(GetInfo.isEmpty())
    {
        QMessageBox::warning(this,QObject::tr("提示"),QObject::tr("该同学未登记"));
        ui->lineStuId->clear();
    }
    else
    {
        ui->labelName->setText(GetInfo[1]);
        ui->labelSex->setText(GetInfo[2]);
        ui->labelTel->setText(GetInfo[3]);
        ui->labelmajor->setText(GetInfo[4]);
        ui->labelClassId->setText(GetInfo[5].left(3));
        ui->labelSeatNum->setText(GetInfo[5].right(3));
        TableHeader <<QObject::tr("登记编号")<<QObject::tr("姓名")<< QObject::tr("性别") << QObject::tr("电话")<<QObject::tr("专业")<<QObject::tr("位置编号")<<QObject::tr("登记时间");
        query = SysDBL::QueryModelView(this,stuid);
        for(int i = 0 ; i < TableHeader.count(); i++)
        {
            query->setHeaderData(i,Qt::Horizontal,TableHeader[i]);//设置显示框表头显示
        }
        ui->tableView->setModel(query);
    }
}

void MainForm::on_BtnDelete_clicked()
{
    dialog = new QDialog(this);
    dialog->setWindowTitle(QObject::tr("位置注销"));
    dialog->setFixedSize(500,350);
    QVBoxLayout *vbox = new QVBoxLayout;
    QLabel *label = new QLabel;
    lineEdit = new QLineEdit();
    label->setPixmap(QPixmap("").scaled(90, 90, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation));
    label->setAlignment(Qt::AlignCenter);
    vbox->addWidget(label);

    label = new QLabel;
    label->setText(QString(QObject::tr("请输入登记编号\n\n")));
    label->setAlignment(Qt::AlignCenter);
    label->setStyleSheet("font-size:20px;");
    vbox->addWidget(label);

    lineEdit->setPlaceholderText(QObject::tr("请输入ID"));
    vbox->addWidget(lineEdit);

    Btn =new QPushButton (dialog);
    Btn->setText(QObject::tr("确认"));
    Btn->move(200,250);
    connect(Btn,&QPushButton::clicked,this,&MainForm::DeleteReg);
    vbox->addStretch();
    dialog->setLayout(vbox);
    dialog->setWindowModality(Qt::ApplicationModal);//阻塞其他窗体
    dialog->show();
}

void MainForm::DeleteReg()
{
    QString ID = lineEdit->text();
    if(ID.isEmpty())
    {
        QMessageBox::warning(dialog,QObject::tr("警告"),QObject::tr("请输入id再试试！！"));
        ui->textBrowser->append("["+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm")+"] 请输入id再试试！......");
    }
    else
    {
        QMessageBox msgBox;
        msgBox.setText(QObject::tr("尊敬的管理员，你好！"));
        msgBox.setInformativeText(QObject::tr("您确定删除该记录吗?"));
        msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
        msgBox.setDefaultButton(QMessageBox::Ok);
        int ret = msgBox.exec();
        if(ret == QMessageBox::Ok)
        {
            ExecStatus removeReg=SysDBL::RegRemove(ID);
            if(removeReg.Status)
            {
                QMessageBox::warning(this,QObject::tr("提示"),QObject::tr("id：%1 移除成功").arg(ID));
                query->clear();
                ui->tableView->setModel(query);
                ui->labelName->clear();
                ui->labelSex->clear();
                ui->labelmajor->clear();
                ui->labelClassId->clear();
                ui->labelSeatNum->clear();
                ui->labelTel->clear();
                ui->lineStuId->clear();
            }
            else
            {
                QMessageBox::warning(this,QObject::tr("提示"),QObject::tr("删除失败，错误反馈如下：\n")+removeReg.StatusInfo);
            }
        }
        ui->textBrowser->append("["+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm")+"] 假设此处有操作日志输出......");
    }
//    qDebug()<<ID;

}
